<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2016/5/29
  Time: 8:33
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="/wechat/home/highSty.css">
    <jsp:include page="../public/head.jsp" flush="true" />
</head>
<body>
    <jsp:include page="../public/header.jsp" flush="true" />
    <div class="my_highsty">
        <!-- 导航 -->
        <section >
            <div class="out">
                <div class="time"><i class="iconfont icon-me">&#xe626;</i><span class="s1">打烊倒计时:</span><span class="s2">${productClass.closetime}</span></div>
                <div class="sold"><span class="s1">销量</span><span class="s2">${scout}</span></div>
            </div>
            <!--/out-->
            <div class="out2">请选择送餐时间</div>
            <!--/out2-->
            <form id="week" action="/home/shopCar.do" method="post" class="out3" >
                <c:forEach items="${week}" var="item" varStatus="status">
                    <%--<div>${item.weekName} ${item.date}<input type="radio" name="week" ${item.state} value="${item.rdate}" ></div>--%>
                    <div class="cont">
                        <input class="myDate" type="radio" name="week" id="radio${status.index}" ${item.state} value="${item.rdate}" onclick="check('i${status.index}')"  onchange="change(this)" >
                        <!--&#xe61c;-->
                        <span class="s1">${item.weekName}</span><span class="s2">${item.date} <i class="icon ${item.state}" id="i${status.index}"></i></span>
                    </div>
                </c:forEach>
                <input type="hidden" name="order" id="order">
                <input  name="totalCount" type="hidden" id="totalCount">
                <input  name="totalPrice" type="hidden" id="totalPrice">
            </form>

            <div class="cau_pos" >
                <c:forEach items="${products}" var="item" varStatus="status">
                    <form id="form${status.index}" >
                            <div class="wrap">
                                <div class="cont">
                                    <div class="pic">
                                        <a><img src="${item.image}" ></a>
                                        <div class="mask"></div>
                                        <div class="picname"><a>${item.name}</a></div>
                                    </div>
                                    <div class="info">

                                        <div class="txt1"><div class="price1">${item.des}<%--<span>(限10份)</span> --%></div></div>
                                        <div class="txt2"><div class="price1"><span class="s1">￥${item.price}</span>/份</div>

                                            <div class="num">
                                                <div onclick="countAdd('${status.index}')" class="n3" style="border-radius: 1.5rem;width: 7rem;font-size:2.2rem;" >购买</div>

                                            </div></div>
                                    </div>
                                </div>
                                <!--/cont-->
                            </div>
                            <input type="hidden" value="${item.price}" name="goodPrcie" id="goodPrcie${status.index}" />
                            <input type="hidden" value="${item.id}" name="goodId" />
                            <input type="hidden" value="${item.image}" name="goodNormalImage" />
                            <input type="hidden" value="${item.name}" name="goodName" />
                                <input type="hidden" value="${item.productClassId}" name="productClassId" />
                            <input type="hidden" value="0" name="goodAccount" id="count${status.index}" />
                    </form>
                </c:forEach>
            </div>
            <!--/out4-->
            <div class="ps" id="pullRefresh">正在加载</div>
            <span class="s2" id="total" hidden>0.0</span>
            <span class="no" id="count" hidden>0</span>
            <%--<div class="opra">--%>
                <%--<input type="button" name="" class="btn3" value="立即购买" onclick="buy()">--%>
            <%--</div>--%>
        </section>
    </div>

<script>
    var isLast = false;
    var index = 0;
    var size = 100;






    function init() {
        $.post("/mobile/product?action=productType",{areaId:${area.id},classId:${productClass.id},begin:index,end:size},function(data){
            if (!data.success){
                tip("",data.msg,true);
            }else {
                if (data.data.length > 0) {
                    $("#pullRefresh").toggle();
                    var html = "";
                    $.each(data.data, function (i, value) {
                        html += '<form id="form' + (index + i) + '" >' +
                                '<div class="cont">' +
                                '<img src="' + value.image  +
                                '<input type="hidden" value="' + value.price + '" name="goodPrcie" id="goodPrcie' + (index + i) + '" />' +
                                '<input type="hidden" value="' + value.id + '" name="goodId" />' +
                                '<input type="hidden" value="' + value.image + '" name="goodNormalImage" />' +
                                '<input type="hidden" value="' + value.name + '" name="goodName" />' +
                                '<input type="hidden" value="0" name="goodAccount" id="count' + (index + i) + '" />' +
                                '<span id="colBt">' +
                                '<i class="iconfont num _num" id="addSub' + (index + i) + '" style="display: none">' +
                                '<div class="mask" ></div>' +
                                '<div class="add _add">' +
                                '<div class="left" onclick="countSub(\'' + (index + i) + '\')" ></div>' +
                                '<span  class="center" id="scount' + (index + i) + '" >0</span>' +
                                '<div class="right" onclick="countAdd(\'' + (index + i) + '\')"></div>' +
                                '</div>' +
                                '</i>' +
                                '<i class="iconfont num _num" id="add' + (index + i) + '" onclick="countAdd(\'' + (index + i) + '\')" ></i>' +
                                '</span>' +
                                '</div>' +
                                '</form>';
                    });
                    $(".out4").append(html);
                }
            }
        },"json");
    }


    <%--$(window).scroll(function(){--%>
        <%--var scrollTop = $(this).scrollTop();--%>
        <%--var scrollHeight = $(document).height();--%>
        <%--var windowHeight = $(this).height();--%>
        <%--if(scrollTop + windowHeight == scrollHeight){--%>
            <%--if (!isLast){--%>
                <%--index = index+size;--%>
                <%--$("#pullRefresh").toggle();--%>
                <%--$.post("/mobile/product?action=productType",{areaId:${area.id},classId:${productClass.id},begin:index,end:size},--%>
                        <%--function(data){--%>
                            <%--if (!data.success){--%>
                                <%--tip("",data.msg,true);--%>
                            <%--}else {--%>
                                <%--if (data.data.length > 0) {--%>
                                    <%--$("#pullRefresh").toggle();--%>
                                    <%--var html = "";--%>
                                    <%--$.each(data.data, function (i, value) {--%>
                                        <%--html +=--%>
                                        <%--'<form id="form' + (index + i) + '" >'+--%>
                                        <%--'<div class="wrap">'+--%>
                                        <%--'<div class="cont">'+--%>
                                        <%--'<div class="pic">'+--%>
                                        <%--'<a><img src="' + value.image + '" onclick="productDetail(' + value.id + ')"></a>'+--%>
                                        <%--'<div class="mask"></div>'+--%>
                                        <%--'<div class="picname"><a>' + value.name + '</a></div>'+--%>
                                        <%--'</div>'+--%>
                                        <%--'<div class="info">'+--%>
                                        <%--'<div class="txt1"><div class="price1">' + value.des + '&lt;%&ndash;<span>(限10份)</span> &ndash;%&gt;</div><div class="sold"></div></div>'+--%>
                                        <%--'<div class="txt2"><div class="price1"><span class="s1">￥' + value.price + '</span>/份</div>'+--%>
                                        <%--'<div class="num">'+--%>
                                        <%--'<div class="n1 current" onclick="countSub(\'' + (index + i) + '\')">-</div>'+--%>
                                        <%--'<div class="n2" id="scount' + (index + i) + '">0</div>'+--%>
                                        <%--'<div class="n3" onclick="countAdd(\'' + (index + i) + '\')">+</div>'+--%>
                                        <%--'</div></div>'+--%>
                                        <%--'</div>'+--%>
                                        <%--'</div>'+--%>
                                        <%--'</div>'+--%>
                                        <%--'<input type="hidden" value="' + value.price + '" name="goodPrcie" id="goodPrcie' + (index + i) + '" />'+--%>
                                        <%--'<input type="hidden" value="' + value.id + '" name="goodId" />'+--%>
                                        <%--'<input type="hidden" value="' + value.image + '" name="goodNormalImage" />'+--%>
                                        <%--'<input type="hidden" value="' + value.name + '" name="goodName" />'+--%>
                                        <%--'<input type="hidden" value="0" name="goodAccount" id="count' + (index + i) + '" />'+--%>
                                        <%--'</form>';--%>
                                    <%--});--%>
                                    <%--$(".cau_pos").append(html);--%>
                                <%--}else{--%>
                                    <%--isLast = true;--%>
                                    <%--$("#pullRefresh").toggle();--%>
                                    <%--$("#pullRefresh").text("已经加载全部");--%>
                                <%--}--%>
                            <%--}--%>
                        <%--},"json");--%>
            <%--}--%>
        <%--}--%>
    <%--});--%>


    var canadd = true;

    $(function () {
        var now = new Date();
        var time = new Date(now.getFullYear()+'/'+(now.getMonth()+1)+"/"+now.getDate()+" ${productClass.closetime}");

        if (now > time){
            alert("商铺已打烊");
            canadd = false;
            $(".opra").hide();
        }
    });

    function countAdd(id) {
        if (!canadd){
            alert("商铺已打烊");
            return;
        }

        var current = $('#count'+id).val();
        var currentCount = $("#count").text();
        var currenttotal = $("#total").text();
        var price = $('#goodPrcie'+id).val();
        current++;
        currentCount++;
        currenttotal = (parseFloat(price)+parseFloat(currenttotal)).toFixed(1);
        $("#count"+id).val(current);
        $("#scount"+id).text(current) ;
        $("#count").text(currentCount);
        $("#total").text(currenttotal);

        if (current == 1){
            $("#add"+id).toggle();
            $("#addSub"+id).toggle();
        }
        buy();
    }
    function countSub(id) {
        if (!canadd){
            alert("商铺已打烊");
            return;
        }

        var current = $('#count'+id).val();
        var currentCount = $("#count").text();
        var currenttotal = $("#total").text();
        if (current == 0)
                return;
        current--;
        currentCount--;
        currenttotal = (parseFloat(currenttotal) - parseFloat($('#goodPrcie'+id).val())).toFixed(1);
        $("#count"+id).val(current);
        $("#scount"+id).text(current) ;
        $("#count").text(currentCount);
        $("#total").text(currenttotal);

        if (current == 0){
            $("#add"+id).toggle();
            $("#addSub"+id).toggle();
        }
    }

    function buy(){
        var forms = $("form:not([id='week'])");
        var goods = [];
        var tmp = $("#week").serializeObject();
        var key = tmp.week;
        var size = forms.size();

        for(var i = 0;i<size;i++){
            var temp = $("#"+forms[i].id).serializeObject();
            if (temp.goodAccount > 0) {
                temp.shopId = key;
                goods.push(temp);
            }
        }
        if (goods.length > 0) {
            var data = '{"' + key + '":' + JSON.stringify(goods) + '}';
            $("#order").val(data);
            $("#totalCount").val($("#count").text());
            $("#totalPrice").val($("#total").text());
            $("#week").submit();
        }
    }

    function check(id) {
        $("i").removeClass("checked");
        $("#"+id).addClass("checked");
    }

    function change(item){

        //现在的时间
        var now = new Date();
        //打烊的时间
        var time = new Date(now.getFullYear()+'/'+(now.getMonth()+1)+"/"+now.getDate()+" ${productClass.closetime}");

        //选择送达的时间
        var selectday = $(item).val();
        var selectdate = new Date(selectday);
        var select =  selectdate.getDate();
        //不允许的情况:   现在的时间与送达的时间一样且 打烊了
        //允许的情况
        var nextdate = new Date((now.valueOf()+1*24*60*60*1000));

        if (now > time &&  nextdate.getDate() == select){
            alert("商铺已打烊");
            canadd = false;
            $(".buy").hide();
        }else{
            $(".buy").show();
            canadd = true;
        }

    }

    $.fn.serializeObject = function()
    {
        var o = {};
        var a = this.serializeArray();
        $.each(a, function() {
            if (o[this.name]) {
                if (!o[this.name].push) {
                    o[this.name] = [o[this.name]];
                }
                o[this.name].push(this.value || '');
            } else {
                o[this.name] = this.value || '';
            }
        });
        return o;
    };




</script>
</body>

</html>
